--探敌度势
local tandiduoshi = fk.CreateSkill {
    name = "mcdh_tandiduoshi"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

tandiduoshi:addEffect(fk.EventPhaseStart, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and player.phase == Player.Play and
                table.find(player.room:getOtherPlayers(player), function(p) return player:canPindian(p) end)
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local can_pindian = table.filter(room:getOtherPlayers(player), function(p) return player:canPindian(p) end)
        local tos_id,card_id = room:askForChooseCardAndPlayers(player,table.map(can_pindian,Util.IdMapper),1,1,".|.|.|hand","#mcdh_tandiduoshi_askforchoose",self.name,true)
        if #tos_id > 0 then
            self.cost_data = {tos_id,card_id}
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = room:getPlayerById(self.cost_data[1][1])
        local card = Fk:getCardById(self.cost_data[2])
        local pindian = player:pindian({to},self.name,card)
        if pindian.results[to.id].winner == player then
            for i = 1, 2, 1 do
                if to:isAlive() and player:isAlive() then
                    local use = room:askForUseCard(player,"slash",nil,"#mcdh_tandiduoshi_askforuse:"..to.id,true,{bypass_times = true,must_targets = {to.id},bypass_distances = true})
                    if use then
                        use.extraUse = true
                        room:useCard(use)
                        if i == 2 then
                            if player:isAlive() then
                                player:drawCards(2,self.name)
                            end
                            break
                        end
                    else
                        if i > 1 then
                            if player:isAlive() then
                                player:drawCards(i-1,self.name)
                            end
                            break
                        end
                    end
                elseif player:isAlive() and i > 1 then
                    player:drawCards(i-1,self.name)
                    break
                else
                    break
                end
            end
        else
            if to:isAlive() then
                to:drawCards(1,self.name)
            end
            if player:isAlive() then
                player:drawCards(1,self.name)
                if to:isAlive() then
                    room:setPlayerMark(to,"@@mcdh_tandiduoshi_banslash",1)
                end
                room:setPlayerMark(player,"@@mcdh_tandiduoshi_banslash",1)
            end
        end
    end
})

local skill = {
    can_refresh = function (self, event, target, player, data)
        if event == fk.PindianResultConfirmed then
            return data.reason == self.name and data.from == player and data.winner ~= player
        else
            return target == player and player:hasSkill(self.name, false, true) and table.find(player.room.players,function (p)
                return p:getMark("@@mcdh_tandiduoshi_banslash") ~= 0
            end)
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        if event == fk.PindianResultConfirmed then
            local from = data.from
            local to = data.to
            local fromcard = data.fromCard
            local tocard = data.toCard
            if to:isAlive() and room:getCardArea(fromcard) == Card.Processing then
                room:obtainCard(to, fromcard, true, fk.ReasonJustMove,player.id,self.name)
            end
            if from:isAlive() and room:getCardArea(tocard) == Card.Processing then
                room:obtainCard(from, tocard, true, fk.ReasonJustMove,player.id,self.name)
            end
        else
            for _, t in ipairs(room.players) do
                if t:getMark("@@mcdh_tandiduoshi_banslash") > 0 then
                    room:setPlayerMark(t,"@@mcdh_tandiduoshi_banslash",0)
                end
            end
        end
    end
}

tandiduoshi:addEffect(fk.TurnStart,skill)
tandiduoshi:addEffect(fk.BuryVictim,skill)
tandiduoshi:addEffect(fk.PindianResultConfirmed,skill)

tandiduoshi:addEffect('prohibit',{
    prohibit_use = function(self, player, card)
        if card then
            return player:getMark("@@mcdh_tandiduoshi_banslash") > 0 and card.trueName == "slash" and card.color == Card.Black
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_tandiduoshi"] = "度势",
    [":mcdh_tandiduoshi"] = "『探敌度势』<br>度势：出牌阶段开始时，你可以拼点，若你赢，你可以对其使用至多两张【杀】，然后摸等量张牌；没赢，你与其获得对方的拼点牌并各摸一张牌，然后直到你的下个回合开始均不能使用黑色【杀】。",
    ["#mcdh_tandiduoshi_askforchoose"] = "探敌度势：选择一张牌与一名角色拼点",
    ["#mcdh_tandiduoshi_askforuse"] = "探敌度势：你可以对 %src 使用一张【杀】",
    ["#mcdh_tandiduoshi_prohibit"] = "度势",
    ["@@mcdh_tandiduoshi_banslash"] = "探敌度势 禁用"
}

return tandiduoshi